package logic

import (
	"context"
	"fmt"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *personrpc.LoginRequest) (*personrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var person mysql.User
	mysql.DB.Debug().Where("account = ?", in.Account).Find(&person)
	if person.ID == 0 {
		return nil, fmt.Errorf("账号未注册，请前往注册")
	}
	if person.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &personrpc.LoginResponse{Id: int64(person.ID)}, nil
}
